package com.ycx.ycxmybatismanagement.controller;


import com.ycx.ycxmybatismanagement.aop.MyLog;
import com.ycx.ycxmybatismanagement.dto.DtoDept;
import com.ycx.ycxmybatismanagement.pojo.Dept;
import com.ycx.ycxmybatismanagement.pojo.Result;
import com.ycx.ycxmybatismanagement.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class DeptController {
    @Autowired
    DeptService deptService;

    /**
     * 查询
     * @return
     */
    @GetMapping("/depts")

    public Result<List<Dept>> selectDeptList(HttpServletRequest request){
//        //token验证
//        String token = request.getHeader("token");
//        //判断token是否为空
//        if (StringUtils.isEmpty(token)){
//            return Result.error("登录后再访问");
//        }
//        //解析token
//        Claims claims = JwtUtils.parseJWT(token);
//        Integer id = claims.get("id", Integer.class);
//        String username = claims.get("username", String.class);
//        String name = claims.get("name", String.class);
//        if (id ==null || id <=0){
//            return Result.error("登录后再访问");
//        }

        List<Dept> deptList=  deptService.deptList();
      return Result.success(deptList);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/depts")
    @MyLog
//    public Result deleteDept(HttpServletRequest request){   1.
    public Result deleteDept(@RequestParam("id")Integer id){  //2.
//    public Result deleteDept(Integer id){       3.定义方法形参
//        String id = request.getParameter("id");
       deptService.deleteDept(id);
        return Result.success();
    }

    /**
     * 新增
     * @param dtoDept
     * @return
     */
    @PostMapping("/depts")
    @MyLog
    public Result insertDept(@RequestBody DtoDept dtoDept){
        deptService.insertDept(dtoDept);
        return Result.success();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("depts/{id}")
    public Result<Dept> selectDept(@PathVariable Integer id){
       Dept dept = deptService.selectDept(id);
        return Result.success(dept);
    }

    /**
     * 修改
     * @param dtoDept
     * @return
     */
    @PutMapping("/depts")
    @MyLog
    public Result updateDept(@RequestBody DtoDept dtoDept){
        deptService.updateDept(dtoDept);
        return Result.success();
    }



}
